package cn.wit.summit.chwan.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.AdPosition;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.models.dto.DiscoveryType;
import cn.wit.summit.chwan.ui.activity.GameListActivity;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.MyImageLoader;

/**
 * Created by tangbl on 2017/9/11.
 */

public class GameLibraryTopAdapter extends BaseAdapter {

    private Context mContext;
    private SparseArray<DiscoveryType> itemBeans;

    public GameLibraryTopAdapter(Context mContext, SparseArray<DiscoveryType> itemBeans) {
        this.mContext = mContext;
        this.itemBeans = itemBeans;
    }

    public void clearData() {
        itemBeans.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (itemBeans==null) {
            return 0;
        }
        return itemBeans.size();
    }

    @Override
    public DiscoveryType getItem(int position) {
        return itemBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        MyViewHolder myViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.find_fragment_game_type_item, null);

            myViewHolder = new MyViewHolder();
            myViewHolder.icon = convertView.findViewById(R.id.icon);
            myViewHolder.name = convertView.findViewById(R.id.name);
            myViewHolder.itemTopTypeRootCl = convertView.findViewById(R.id.itemTopTypeRootCl);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        final DiscoveryType type = itemBeans.get(position);
        myViewHolder.icon.setImageURI(MyImageLoader.makeUri(mContext, type.getResId()));
        myViewHolder.name.setText(type.getName());
        myViewHolder.itemTopTypeRootCl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.getTypeId() == -1) return;
                if (type.getTypeId()==999) {
                    mContext.startActivity(new Intent(mContext, GameListActivity.class));
                } else {
                    IntentUtil.getInstance().goDiscoveryListActivity(v.getContext(), type.getTypeId(), type.getName());
                    StatFactory.getInstance().sendClickAdvertSpacePage(v.getContext(), AdPosition.getModeByEnum(6), AdPosition.getNameByEnum(6), "", "", position + 1);
                }
            }
        });


        return convertView;
    }

    public class MyViewHolder {
        public ConstraintLayout itemTopTypeRootCl;
        public SimpleDraweeView icon;
        public TextView         name;
    }
}
